package top.eggcode.plugins.session;

import lombok.Data;
import top.eggcode.plugins.session.manager.SessionManager;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;

/**
 * Title:
 * Description: TODO
 * Date: 2021/6/13 16:49
 *
 * @author JiaQi Ding
 * @version 1.0
 */
@Data
public class ProxySession implements Session {

    private final Serializable key;
    private final transient SessionManager sessionManager;
    private Date startTimestamp = null;
    private String host = null;

    public ProxySession(SessionManager sessionManager, Serializable key) {
        this.sessionManager = sessionManager;
        this.key = key;
    }

    @Override
    public Serializable getId() {
        return key;
    }

    @Override
    public Date getStartTimestamp() {
        return null;
    }

    @Override
    public Date getLastAccessTime() {
        return null;
    }

    @Override
    public long getTimeout() {
        return this.sessionManager.getTimeout(this.key);
    }

    @Override
    public void setTimeout(long maxIdleTimeInMillis) {
        this.sessionManager.setTimeout(this.key, maxIdleTimeInMillis);
    }

    @Override
    public String getHost() {
        return null;
    }

    @Override
    public void touch() {
        this.sessionManager.touch(key);
    }

    @Override
    public void stop() {
        this.sessionManager.stop(this.key);
    }

    @Override
    public Collection<Object> getAttributeKeys() {
        return null;
    }

    @Override
    public Object getAttribute(Object attributeKey) {
        return this.sessionManager.getAttribute(this.key, attributeKey);
    }

    @Override
    public void setAttribute(Object attributeKey, Object value) {
        if (value == null) {
            this.removeAttribute(attributeKey);
        } else {
            this.sessionManager.setAttribute(this.key, attributeKey, value);
        }
    }

    @Override
    public Object removeAttribute(Object attributeKey) {
        return this.sessionManager.removeAttribute(this.key, attributeKey);
    }
}
